package com.ruoyi.common.utils.oss;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Author: yan
 * @Description:
 * @Date: create in 2021/12/14 11:42
 */
public class DateUtils extends org.apache.commons.lang3.time.DateUtils {

    public static final String PATTERN_STANDARD = "yyyy-MM-dd HH:mm:ss";

    public static final String PATTERN_DATE = "yyyy-MM-dd";

    public static final String PARRERN_Hour_Minute = "HH:mm";

    private static String[] parsePatterns = { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
            "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM", "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss",
            "yyyy.MM.dd HH:mm", "yyyy.MM" };

    /**
     * 得到几天前的时间
     *
     * @param d
     * @param day
     * @return
     */
    public static Date getDateBefore(Date d, int day) {
        Calendar no = Calendar.getInstance();
        no.setTime(d);
        no.set(Calendar.DATE, no.get(Calendar.DATE) - day);
        return no.getTime();
    }

    /**
     * 得到当前日期字符串 格式（yyyy-MM-dd）
     */
    public static String getDate() {
        return getDate("yyyy-MM-dd");
    }

    /**
     * 得到当前日期字符串 格式（yyyy-MM-dd） pattern可以为："yyyy-MM-dd" "HH:mm:ss" "E"
     */
    public static String getDate(String pattern) {
        return DateFormatUtils.format(new Date(), pattern);
    }

    /**
     * 得到日期字符串 默认格式（yyyy-MM-dd） pattern可以为："yyyy-MM-dd" "HH:mm:ss" "E"
     */
    public static String formatDate(Date date, Object... pattern) {
        String formatDate = null;
        if (pattern != null && pattern.length > 0) {
            formatDate = DateFormatUtils.format(date, pattern[0].toString());
        } else {
            formatDate = DateFormatUtils.format(date, "yyyy-MM-dd");
        }
        return formatDate;
    }

    /**
     * 得到日期时间字符串，转换格式（yyyy-MM-dd HH:mm:ss）
     */
    public static String formatDateTime(Date date) {
        return formatDate(date, "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * 得到当前时间字符串 格式（HH:mm:ss）
     */
    public static String getTime() {
        return formatDate(new Date(), "HH:mm:ss");
    }

    /**
     * 得到当前日期和时间字符串 格式（yyyy-MM-dd HH:mm:ss）
     */
    public static String getDateTime() {
        return formatDate(new Date(), "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * 得到当前年份字符串 格式（yyyy）
     */
    public static String getYear() {
        return formatDate(new Date(), "yyyy");
    }

    /**
     * 得到当前月份字符串 格式（MM）
     */
    public static String getMonth() {
        return formatDate(new Date(), "MM");
    }

    /**
     * 得到当天字符串 格式（dd）
     */
    public static String getDay() {
        return formatDate(new Date(), "dd");
    }

    /**
     * 得到当前星期字符串 格式（E）星期几
     */
    public static String getWeek() {
        return formatDate(new Date(), "E");
    }

    public static int getWeekInt(){
        String week = getWeek();
        week = week.substring(week.length()-1, week.length());
        int weekInt = 0;
        if("一".equals(week)){
            return weekInt = 1;
        }else if("二".equals(week)){
            return weekInt = 2;
        }else if("三".equals(week)){
            return weekInt = 3;
        }else if("四".equals(week)){
            return weekInt = 4;
        }else if("五".equals(week)){
            return weekInt = 5;
        }else if("六".equals(week)){
            return weekInt = 6;
        }else {
            return weekInt = 7;
        }
    }

    /**
     * 日期型字符串转化为日期 格式 { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm",
     * "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy.MM.dd",
     * "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm" }
     */
    public static Date parseDate(Object str) {
        if (str == null) {
            return null;
        }
        try {
            return parseDate(str.toString(), parsePatterns);
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * 获取过去的天数
     *
     * @param date
     * @return
     */
    public static long pastDays(Date date) {
        long t = System.currentTimeMillis() - date.getTime();
        return t / (24 * 60 * 60 * 1000);
    }

    /**
     * 获取过去的小时
     *
     * @param date
     * @return
     */
    public static long pastHour(Date date) {
        long t = System.currentTimeMillis()  - date.getTime();
        return t / (60 * 60 * 1000);
    }

    /**
     * 获取过去的分钟
     *
     * @param date
     * @return
     */
    public static long pastMinutes(Date date) {
        long t = System.currentTimeMillis()  - date.getTime();
        return t / (60 * 1000);
    }

    /**
     * 转换为时间（天,时:分:秒.毫秒）
     *
     * @param timeMillis
     * @return
     */
    public static String formatDateTime(long timeMillis) {
        long day = timeMillis / (24 * 60 * 60 * 1000);
        long hour = (timeMillis / (60 * 60 * 1000) - day * 24);
        long min = ((timeMillis / (60 * 1000)) - day * 24 * 60 - hour * 60);
        long s = (timeMillis / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
        long sss = (timeMillis - day * 24 * 60 * 60 * 1000 - hour * 60 * 60 * 1000 - min * 60 * 1000 - s * 1000);
        return (day > 0 ? day + "," : "") + hour + ":" + min + ":" + s + "." + sss;
    }

    /**
     * 获取两个日期之间的天数
     *
     * @param before
     * @param after
     * @return
     */
    public static int getDistanceOfTwoDate(Date before, Date after) {
        long beforeTime = before.getTime();
        long afterTime = after.getTime();
        return (int) ((afterTime - beforeTime) / (1000 * 60 * 60 * 24));
    }

    /**
     * 字符串转日期
     *
     * @param strDate
     *            字符串日期
     * @param pattern
     *            需要转换成的类型（YYYY-mm-dd）
     * @return
     */
    public static Date string2Date(String strDate, String pattern) {
        if (strDate == null || "".equals(strDate)) {
            throw new RuntimeException("str date null");
        }
        if (pattern == null || "".equals(pattern)) {
            pattern = DateUtils.PATTERN_DATE;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        Date date = null;

        try {
            date = sdf.parse(strDate);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        return date;
    }
    //根据给定的时间算出间隔天数
    public static String getBetweenDay(Date startDate,Date endDate){
        long res=endDate.getTime()-startDate.getTime();
        res=res/24/60/60/1000;
        return Long.toString(res);
    }
    //获取距今7天每天的日期，转换成string
    public static List<String> getDateStringList(){
        long time=86400000;
        Date date=new Date();
        List<String> list=new ArrayList<String>();
        for (int i = 6; i >0 ; i--) {
            Date date2=new Date();
            long t=date.getTime()-time*i;
            date2.setTime(t);
            list.add(DateUtils.formatDateTime(date2));
        }
        list.add(DateUtils.formatDateTime(date));
        return list;
    }
    public static List<Date> getDateList(Date before, Date after, String str,String medicine_cycle) {
        long frequency=1;
        if ("medicine_cycle01".equals(medicine_cycle)) {
            frequency=1;
        }else if("medicine_cycle02".equals(medicine_cycle)){
            frequency=2;
        }else if("medicine_cycle03".equals(medicine_cycle)){
            frequency=3;
        }else if("medicine_cycle04".equals(medicine_cycle)){
            frequency=7;
        }else if("medicine_cycle05".equals(medicine_cycle)){
            frequency=14;
        }else if("medicine_cycle06".equals(medicine_cycle)){
            frequency=30;
        }
        List<Date> dates = new ArrayList<Date>();
        long beforeTime = before.getTime();
        long afterTime = after.getTime();
        long day=1000 * 60 * 60 * 24*frequency;
        int days = (int) ((afterTime - beforeTime) / (1000 * 60 * 60 * 24*frequency));
        if (days>0) {
            for (int i = 0; i <=days; i++) {
                if (null != str && !"".equals(str)) {
                    if (str.contains(";")) {
                        String[] strings = str.split(";");
                        for (String string : strings) {
                            if (string.contains(":")) {
                                String[] strings2 = string.split(":");
                                Integer hour = Integer.parseInt(strings2[0]);
                                Integer min = Integer.parseInt(strings2[1]);
                                long time = 1000 * 60 * 60 * hour + 1000 * 60 * min;
                                Date date=new Date();
                                if (i==0) {
                                    date.setTime(beforeTime+time);
                                    dates.add(date);
                                }else {
                                    beforeTime=beforeTime+day;
                                    date.setTime(beforeTime+time);
                                    dates.add(date);
                                }
                            } else if (string.contains("：")) {
                                String[] strings2 = string.split("：");
                                Integer hour = Integer.parseInt(strings2[0]);
                                Integer min = Integer.parseInt(strings2[1]);
                                long time = 1000 * 60 * 60 * hour + 1000 * 60 * min;
                                Date date=new Date();
                                if (i==0) {
                                    date.setTime(beforeTime+time);
                                    dates.add(date);
                                }else{
                                    beforeTime=beforeTime+day;
                                    date.setTime(beforeTime+time);
                                    dates.add(date);
                                }
                            }
                        }
                    } else {
                        if (str.contains(":")) {
                            String[] strings2 = str.split(":");
                            Integer hour = Integer.parseInt(strings2[0]);
                            Integer min = Integer.parseInt(strings2[1]);
                            long time = 1000 * 60 * 60 * hour + 1000 * 60 * min;
                            Date date=new Date();
                            if (i==0) {
                                date.setTime(beforeTime+time);
                                dates.add(date);
                            }else{
                                beforeTime=beforeTime+day;
                                date.setTime(beforeTime+time);
                                dates.add(date);
                            }

                        } else if (str.contains("：")) {
                            String[] strings2 = str.split("：");
                            Integer hour = Integer.parseInt(strings2[0]);
                            Integer min = Integer.parseInt(strings2[1]);
                            long time = 1000 * 60 * 60 * hour + 1000 * 60 * min;
                            Date date=new Date();
                            if (i==0) {
                                date.setTime(beforeTime+time);
                                dates.add(date);
                            }else{
                                beforeTime=beforeTime+day;
                                date.setTime(beforeTime+time);
                                dates.add(date);
                            }
                        }
                    }
                }
            }
        }else{
            if (null != str && !"".equals(str)) {
                if (str.contains(";")) {
                    String[] strings = str.split(";");
                    for (String string : strings) {
                        if (string.contains(":")) {
                            String[] strings2 = string.split(":");
                            Integer hour = Integer.parseInt(strings2[0]);
                            Integer min = Integer.parseInt(strings2[1]);
                            long time = 1000 * 60 * 60 * hour + 1000 * 60 * min;
                            Date date=new Date();
                            date.setTime(beforeTime+time);
                            dates.add(date);
                        } else if (string.contains("：")) {
                            String[] strings2 = string.split("：");
                            Integer hour = Integer.parseInt(strings2[0]);
                            Integer min = Integer.parseInt(strings2[1]);
                            long time = 1000 * 60 * 60 * hour + 1000 * 60 * min;
                            Date date=new Date();
                            date.setTime(beforeTime+time);
                            dates.add(date);
                        }
                    }
                } else {
                    if (str.contains(":")) {
                        String[] strings2 = str.split(":");
                        Integer hour = Integer.parseInt(strings2[0]);
                        Integer min = Integer.parseInt(strings2[1]);
                        long time = 1000 * 60 * 60 * hour + 1000 * 60 * min;
                        Date date=new Date();
                        date.setTime(beforeTime+time);
                        dates.add(date);
                    } else if (str.contains("：")) {
                        String[] strings2 = str.split("：");
                        Integer hour = Integer.parseInt(strings2[0]);
                        Integer min = Integer.parseInt(strings2[1]);
                        long time = 1000 * 60 * 60 * hour + 1000 * 60 * min;
                        Date date=new Date();
                        dates.add(date);
                    }
                }
            }

        }

        return dates;
    }

    /**
     * date 转 String
     * @param date
     * @return
     */
    public static String dateToString(Date date){
        SimpleDateFormat sdf = new SimpleDateFormat(PATTERN_DATE);
        return sdf.format(date);
    }
    public static String dateToStringYMDHMS(Date date){
        SimpleDateFormat sdf = new SimpleDateFormat(PATTERN_STANDARD);
        return sdf.format(date);
    }

    public static String dateToStringYMDHM(Date date){
        SimpleDateFormat sdf = new SimpleDateFormat(PATTERN_DATE + " " +PARRERN_Hour_Minute);
        return sdf.format(date);
    }

    public static Date stringToDateYMDHMS(String str) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(PATTERN_STANDARD);
        return sdf.parse(str);
    }
    public static Date stringToDateYMD(String str) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(PATTERN_DATE);
        return sdf.parse(str);
    }

    /**
     * CTS转UTC时间（加8小时）
     * @param date
     * @return
     */
    public static Date CTSToUTC(Date date){
        //8小时误差修正
        long l=28800000L;
        date.setTime(date.getTime()+l);
        return date;
    }

    public static boolean isDateString(String datevalue, String dateFormat) {
        if (StringUtils.isEmpty(datevalue) || StringUtils.isEmpty(dateFormat)) {
            return false;
        }
        try {
            SimpleDateFormat fmt = new SimpleDateFormat(dateFormat);
            Date dd = fmt.parse(datevalue);
            if (datevalue.equals(fmt.format(dd))) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            return false;
        }
    }


    public static int compare_date(String DATE1, String DATE2) {
        SimpleDateFormat df = new SimpleDateFormat(PATTERN_STANDARD);
        try {
            Date dt1 = df.parse(DATE1);
            Date dt2 = df.parse(DATE2);
            if (dt1.getTime() > dt2.getTime()) {
//          System.out.println("dt1 在dt2前");
                return 1;
            } else if (dt1.getTime() < dt2.getTime()) {
//          System.out.println("dt1在dt2后");
                return -1;
            } else {
                return 0;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return 0;
    }

    public static int compare_date(String DATE1, String DATE2,String foramt) {
        SimpleDateFormat df = new SimpleDateFormat(foramt);
        try {
            Date dt1 = df.parse(DATE1);
            Date dt2 = df.parse(DATE2);
            if (dt1.getTime() > dt2.getTime()) {
//          System.out.println("dt1 在dt2前");
                return 1;
            } else if (dt1.getTime() < dt2.getTime()) {
//          System.out.println("dt1在dt2后");
                return -1;
            } else {
                return 0;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return 0;
    }

    public static String getYesterday(){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date=new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        date = calendar.getTime();
        return sdf.format(date);
    }

}
